package store.lunangangster.engine.impl.el;

import java.util.Map;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.ExpressionFactory;
import javax.el.FunctionMapper;
import javax.el.VariableMapper;
import store.lunangangster.engine.impl.entity.mgr.VariableScope;

public class DefaultContext extends ELContext {

  protected FunctionMapper functionMapper = new DefaultFunctionMapper();
  protected VariableMapper variableMapper = new DefaultVariableMapper();
  protected ELResolver elResolver;
  protected ExpressionFactory expressionFactory;

  public DefaultContext(ELResolver elResolver, VariableScope variableScope, ExpressionFactory expressionFactory) {
    this.elResolver = elResolver;
    this.expressionFactory = expressionFactory;
    setVariables(variableScope.getVariables());
  }

  public void setVariables(Map<String, Object> variables) {
    for (String variableName : variables.keySet()) {
      Object value = variables.get(variableName);
      variableMapper.setVariable(variableName, expressionFactory.createValueExpression(value, Object.class));
    }
  }

  @Override
  public ELResolver getELResolver() {
    return elResolver;
  }

  @Override
  public FunctionMapper getFunctionMapper() {
    return functionMapper;
  }

  @Override
  public VariableMapper getVariableMapper() {
    return variableMapper;
  }
}
